var express = require('express');
var FrontController = require('./src/core/controllers/frontcontroller');
var Facade = require('./src/core/facade/facade');
var app = module.exports = express.createServer();

var handler = new FrontController();

// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function(){
  app.use(express.errorHandler()); 
});


// Routes

app.get('/departamento/:id', function(req, res) {
    handler.handleGetDepartamentoById(req, function(err, doc) {
        res.end(String(doc.getId()));
    });
});

app.get('/curso/:id', function(req, res) {
    handler.handleGetCursoById(req, function(err, doc) {
        res.end(String(doc.getId()));
    });
});

app.get('/projeto/:id', function(req, res) {
    handler.handleGetProjetoById(req, function(err, doc) {
        res.end(String(doc.getId()));
    });
});

app.get('/secretario/:id', function(req, res) {
    handler.handleGetSecretarioById(req, function(err, doc) {
        res.end(String(doc.getId()));
    });
});

app.get('/professor/:id', function(req, res) {
    handler.handleGetProfessorById(req, function(err, doc) {
        res.end(String(doc.getId()));
    });
});

app.get('/disciplina/:id', function(req, res) {
    handler.handleGetDisciplinaById(req, function(err, doc) {
        res.end(String(doc.getId()));
    });
});

app.get('/turma/:id', function(req, res) {
    handler.handleGetTurmaById(req, function(err, doc) {
        res.end(String(doc.getId()));
    });
});

app.get('/search/q=:query', function(req, res) {
    console.log(req.params.query);
});

app.post('/new/departamento', function(req, res) {
    console.log(req.body);
    handler.handleAddDepartamento(req);
    res.end();
});

app.post('/new/projeto', function(req, res) {
    console.log(req.body);
    handler.handleAddProjeto(req);
});

app.post('/new/curso', function(req, res) {
    console.log(req.body);
    handler.handleAddCurso(req);
    res.end();
});

app.post('/new/secretario', function(req, res) {
    console.log(req.body);
    res.end();
});

app.post('/new/professor', function(req, res) {
});

app.post('/new/disciplina', function(req, res) {
    console.log(req.body);
    handler.handleAddDisciplina(req);
    res.end();
});

app.post('/new/turma', function(req, res) {
    console.log(req.body);
    handler.handleAddTurma(req);
    res.end();
});

app.get('/admin', function(req, res) {
    Facade.getAllDisciplinas(function(err, docs) {
        var disciplinas = docs;
        Facade.getAllDepartamentos(function(err, docs) {
            var departamentos = docs;
            Facade.getAllCursos(function(err, docs) {
                var cursos = docs;
                res.render('admin', {
                    title: 'Aloka - Painel administrativo',
                    layout: false,
                    disciplinas: disciplinas,
                    departamentos: departamentos,
                    cursos: cursos
                });
            });
        });
    });
});

app.get('/', function(req, res) {
  res.render('login',{
      title: 'Aloka - Login',
      layout: false
  });
});

app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
